<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Preview Offer (/admin/offer_preview.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";


// Start session if not already started
session_start();

// Require coupon generator function
require_once(dirname(__FILE__)."/function.coupon_generate.php");

if(!isset($_SESSION['login_t'])) {
	/* If we're not logged in, take us to the login page
	   and ensure the rest of the code isn't run */
	header("Location: /admin/login.php");
	exit;
}

$query = "SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE campaign_id = '" . $_GET['campaign_id'] . "'";
$campaign = $db->get_row($query);

if(!$campaign) {
	exit;
}

$query = "SELECT * FROM `" . DB_TBL_PREFIX . "offertypes` WHERE offer_type_id = '" . $_GET['offer_type'] . "'";
$type = $db->get_row($query);

if(!$type) {
	exit;
}

foreach($_GET as $key=>$val) {
	if($key != 'campaign_id') $off[$key] = $val;
}

/* Create offer coupon image. */
$off['offer_end_date'] = $campaign->end_date;
$offer_img = coupon_generate($off, $coupon);

if($offer_img == false) {
	exit;
} else {
	header("Content-Type: image/png");
	echo $offer_img;
}
?>